<?php
/**
 * Created by PhpStorm.
 * User: tengjs
 * Date: 2020/11/3
 * Time: 7:37
 */

namespace App\Http\Controllers\Api\Member;

use App\Admin\Models\ContentModel;
use App\Admin\Models\EmployeeModel;
use App\Admin\Models\PositionModel;
use App\Admin\Models\RecruitmentModel;
use App\Admin\Models\SequenceModel;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Validator;

class MemberController extends BaseController
{
    /**
     * 成员
     *
     * @return array
     */
    public function list()
    {
        $file = [
            'employee_id', 'name_en as name', 'sequence_pid', 'office_address_zh as phd', 'research_direction_zh as msc',
            'engineering as me', 'personal_profile_zh as be', 'email', 'research_direction_en as research_interests', 'phone',
            'personal_homepage as homepage', 'office_address_en  as address', 'personal_profile_en as profile',
            'graduation_destination', 'position_id', 'avatar'
        ];

        $res = EmployeeModel::select($file)
            ->orderBy('order', 'asc')
            ->orderBy('employee_id', 'desc')
            ->get();
        $userList = !empty($res) ? $res->toArray() : [];

        // 岗位分组
        $sequences = [];
        if (!empty($userList)) {
            $sequenceIds = [];
            foreach ($userList as $item) {
                $sequenceIds = array_merge($sequenceIds, $item['sequence_pid']);
            }
            $sequenceIds = array_filter(array_unique($sequenceIds));
            $sequences = SequenceModel::select(['sequence_id', 'name_en as name'])
                ->whereIn('sequence_id', $sequenceIds)
                ->orderBy('order', 'asc')
                ->get();
            if (!empty($sequences)) {
                $sequences = json_decode(json_encode($sequences), true);
            }
        }

        // 学历身份
        $positionOptions = [
            '1' => 'Doctoral Student',
            '2' => 'Master Student',
            '3' => 'Undergraduate Student',
            '4' => 'Visiting Student'
        ];

        // 组装
        $data = [];
        foreach ($sequences as $v) {
            $dataItem = [
                'sequence_id' => $v['sequence_id'],
                'sequence_name' => $v['name'],
                'user_list' => []
            ];
            foreach ($userList as $item) {
                if (empty($item['sequence_pid']) || !in_array($v['sequence_id'], $item['sequence_pid'])) {
                    continue;
                }

                $item['user_id'] = $item['employee_id'];
                $item['avatar'] = !empty($item['avatar']) ? $this->buildImageUrl($item['avatar']) : defaultUserImage();
                $item['profile'] = htmlspecialchars_decode($item['profile']);
                $item['position_name'] = !empty($item['position_id']) ? $positionOptions[$item['position_id']] : '';

                unset($item['employee_id']);
                $dataItem['user_list'][] = $item;
            }
            $data[] = $dataItem;
        }

        // 数据查询
        $result = [
            'code' => 0,
            'msg' => '数据获取成功！',
            'data' => $data
        ];

        return $this->success($result['data'], $result['msg'] ?: '数据获取成功！');
    }
}
